package com.yxt.qmgf.start.ctrl;

import cn.hutool.extra.spring.SpringUtil;
import com.yxt.qmgf.ApplicationStarter;
import com.yxt.qmgf.common.ClientSession;
import com.yxt.qmgf.start.dao.LoginDao;
import com.yxt.qmgf.start.service.LoginService;
import com.yxt.qmgf.common.view.CdzAlert;
import com.yxt.qmgf.start.view.UserCreatWindow;
import com.yxt.qmgf.start.vo.LoginVO;
import com.yxt.qmgf.userinfo.dao.UserInfoDao;
import com.yxt.qmgf.userinfo.vo.UserInfoVO;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class WelcomeController implements Initializable {
    public Button btn_creat;
    @FXML
    private VBox vb_root;
    @FXML
    private ComboBox<String> tf_username;
    @FXML
    private Button btn_Login;
    @FXML
    private PasswordField pf_password;

    @Autowired
    private LoginService loginService;

    public WelcomeController() {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tf_username.setValue("yxt");
        pf_password.setText("yxt20030123");
    }

    @FXML
    protected void login() {
        String username = tf_username.getValue();
        String password = pf_password.getText();
        if ("".equals(username.trim())) {
            new CdzAlert(Alert.AlertType.WARNING, "用户名不能为空").show();
            return;
        }
        if ("".equals(password.trim())) {
            new CdzAlert(Alert.AlertType.WARNING, "密码不能为空").show();
            return;
        }
        btn_Login.setDisable(true);
        btn_Login.setText("用户验证中....");

        LoginVO vo = SpringUtil.getBean(LoginDao.class).verifySelect(username);
        if (vo != null) {
            if (username.equals(vo.getUsername()) && password.equals(vo.getPassword())) {
                ClientSession.addAttribute("user", vo);
                UserInfoVO uvo = SpringUtil.getBean(UserInfoDao.class).SelectByUid(String.valueOf(vo.getUid()));
                ClientSession.addAttribute("user_info", uvo);
                ApplicationStarter.toMainStageView();
            } else {
                btn_Login.setText("登 陆");
                btn_Login.setDisable(false);
                new CdzAlert(Alert.AlertType.INFORMATION, "用户名或密码错误").showAndWait();
            }
        } else {
            // 处理数据库中不存在该账号的情况
            btn_Login.setText("登 陆");
            btn_Login.setDisable(false);
            new CdzAlert(Alert.AlertType.INFORMATION, "该账号不存在，请检查或注册新账号").showAndWait();
        }
    }
    @FXML
    public void creat() {
        UserCreatWindow ucw = new UserCreatWindow();
        Stage UserCreatWindowStage = new Stage();
        ucw.start(UserCreatWindowStage);
    }
}

